﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMIS_ERP.Web.Controls
{
    public partial class Pagination : System.Web.UI.UserControl
    {
        public event ImageClickEventHandler NavigationClick;

        #region 属性

        /// <summary>获取或设置当前显示页的索引
        /// 	<remark>2008-08-08 05信管 </remark>
        /// </summary>
        public int PageCurrent
        {
            get
            {
                return ViewState["PageCurrent"] == null ? 1 : (int)ViewState["PageCurrent"];
            }
            set
            {
                ViewState["PageCurrent"] = value;
                ini_btnNavigation();
            }
        }

        /// <summary>获取或设置总页数
        /// 	<remark>2008-08-08 05信管 </remark>
        /// </summary>
        public int PageSum
        {
            get
            {
                return ViewState["PageSum"] == null ? 1 : (int)ViewState["PageSum"];
            }
            set
            {
                ViewState["PageSum"] = value;
                rgvPage.MaximumValue = (value == 0) ? "1" : value.ToString();
                ini_btnNavigation();
            }
        }

        #endregion 属性

        /// <summary>页面加载事件
        /// 	<remark>2008-08-08 05信管 </remark>
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ini_btnNavigation();
            }
        }

        /// <summary>重写OnInit方法
        /// 	<remark>2008-08-08 05信管 </remark>
        /// </summary>
        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ibtnFirst.Click += new ImageClickEventHandler(btnNavigation_Click);
            ibtnBack.Click += new ImageClickEventHandler(btnNavigation_Click);
            ibtnNext.Click += new ImageClickEventHandler(btnNavigation_Click);
            ibtnLast.Click += new ImageClickEventHandler(btnNavigation_Click);
            ibtnGo.Click += new ImageClickEventHandler(btnNavigation_Click);
        }

        /// <summary>初始化导航按钮——按钮可用性
        /// 	<remark>2008-08-08 05信管 </remark>
        /// </summary>
        private void ini_btnNavigation()
        {
            //全部导航按钮不可用
            ibtnFirst.Enabled = false;
            ibtnBack.Enabled = false;
            ibtnNext.Enabled = false;
            ibtnLast.Enabled = false;
            //判断是不是第一页
            if (PageCurrent > 1)
            {
                ibtnFirst.Enabled = true;
                ibtnBack.Enabled = true;
            }
            //判断是不是最后一页
            if (PageCurrent < PageSum)
            {
                ibtnNext.Enabled = true;
                ibtnLast.Enabled = true;
            }
        }

        /// <summary>响应单击按钮事件——页面导航
        /// 	<remark>2008-08-08 05信管 </remark>
        /// </summary>
        private void btnNavigation_Click(object sender, ImageClickEventArgs e)
        {
            //判断单击的按钮
            ImageButton btn = sender as ImageButton;
            switch (btn.CommandArgument)
            {
                //首页
                case "First":
                    PageCurrent = 1;
                    break;
                //上一页
                case "Previous":
                    PageCurrent -= 1;
                    break;
                //下一页
                case "Next":
                    PageCurrent += 1;
                    break;
                //尾页
                case "Last":
                    PageCurrent = PageSum;
                    break;
                case "Go":
                    if (txtPageCurrent.Text.Trim() == "")
                    {
                        return;
                    }
                    PageCurrent = Convert.ToInt32(txtPageCurrent.Text.Trim());
                    break;
            }
            if (NavigationClick != null)//调用事件 
            {
                NavigationClick(sender, e);
            }
        }


    }
}